﻿Public Partial Class Wfrm_Modificar_OC
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnBuscaOC_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuscaOC.Click
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_Get_OC"
        rp.AddParametro(Val(txtNOc.Text))
        exec.ExecuteDataTable(rp)

        NOC.Text = Val(txtNOc.Text)
        NOC.UpdateAfterCallBack = True

        If Not rp.TieneDatos Then
            Mensaje("Número de Orden No Encontrado")
            img_crear_oc.Visible = false
            img_crear_oc.UpdateAfterCallBack = True
        Else
            img_crear_oc.Visible = True
            img_crear_oc.UpdateAfterCallBack = True
        End If

        Grilla_OC.DataSource = rp.DtTable
        Grilla_OC.DataBind()
        Grilla_OC.UpdateAfterCallBack = True

        Dim i As Integer
        For i = 0 To Me.Grilla_OC.Items.Count - 1
            If rp.DtTable.Rows(i).Item(6) = "0" Then
                DirectCast(Grilla_OC.Items(i).Cells(8).Controls(1), Anthem.TextBox).Text = FormatNumber(rp.DtTable.Rows(i).Item(4) - rp.DtTable.Rows(i).Item(5), 0) 'Val(Grilla_SOL.Items(i).Cells(6).Text) - Val(Grilla_SOL.Items(i).Cells(7).Text)
                DirectCast(Grilla_OC.Items(i).Cells(8).Controls(1), Anthem.TextBox).UpdateAfterCallBack = True

                'le asigno la fila al boton grabar y al eliminar
                '<script></script> 
            End If

            'DirectCast(Grilla_SOL.Items(i).Cells(8).Controls(1), Anthem.TextBox).Attributes.Add("onkeyup", "this.value =FormatNumber(this.value);")
            'DirectCast(Grilla_SOL.Items(i).Cells(8).Controls(1), Anthem.TextBox).Attributes.Add("onkeyup", "this.value=FormatNumber(this.value);")


            'Asignar mejor proveedor
            DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).DataSource = FuncionesGlobal.GetProveedorPrecioOC(Grilla_OC.Items(i).Cells(12).Text, Grilla_OC.Items(i).Cells(13).Text)
            DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).DataValueField = "tRUT_PK"
            DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).DataTextField = "tNombres"
            DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).DataBind()
            DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).UpdateAfterCallBack = True
            ' DirectCast(Grilla_SOL.Items(i).Cells(9).Controls(1), Anthem.DropDownList).ValidationGroup = DirectCast(Grilla_SOL.Items(i).Cells(9).Controls(1), Anthem.DropDownList).ID & "|" & i

            'Precio
            Dim Precio As String
            Try
                Precio = DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).SelectedItem.Text
            Catch ex As Exception
                Precio = "$ 0"
            End Try

            If Val(DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text) = 0 Then
                DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text = Precio.Split(" ")(1)
                DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).UpdateAfterCallBack = True
            End If



            'Dim script As String = "return confirm('¿Esta Seguro(a) de Eliminar el Producto de la Solicitud?');"
            'DirectCast(Grilla_SOL.Items(i).Cells(2).Controls(1), Anthem.ImageButton).Attributes.Add("onclick", script)
            ''Anthem.Manager.AddScriptForClientSideEval("$('" & DirectCast(Grilla_SOL.Items(i).Cells(2).Controls(1), Anthem.ImageButton).ClientID & "').bind('copy', function(){return false;});")
            ''Anthem.Manager.AddScriptForClientSideEval("$('" & DirectCast(Grilla_SOL.Items(i).Cells(2).Controls(1), Anthem.ImageButton).ClientID & "').bind('paste', function(){return false;});")
            'DirectCast(Grilla_SOL.Items(i).Cells(2).Controls(1), Anthem.ImageButton).CommandName = i
            ''DirectCast(Grilla_SOL.Items(i).Cells(10).Controls(1), Anthem.ImageButton).CommandName = i
        Next


    End Sub
    Sub Mensaje(ByVal Msg As String)

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & Msg & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & Msg & "')</script>")
        End If

    End Sub

    Protected Sub img_crear_oc_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles img_crear_oc.Click
        Dim EXEC As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence


        Dim i As Integer
        For i = 0 To Me.Grilla_OC.Items.Count - 1

            Dim CtComprada As Decimal = DirectCast(Grilla_OC.Items(i).Cells(7).Controls(1), Label).Text
            Dim CtOC As Decimal = Grilla_OC.Items(i).Cells(14).Text
            Dim CtNueva As Decimal = DirectCast(Grilla_OC.Items(i).Cells(8).Controls(1), Anthem.TextBox).Text
            Dim CtSolicitud As Decimal = DirectCast(Grilla_OC.Items(i).Cells(6).Controls(1), Label).Text

            If CtComprada - CtOC + CtNueva > CtSolicitud Then
                Mensaje("ERROR: La Cantidad Ingresada Supera el Máximo Permitido")
                EXEC.Rollback()
                Exit Sub
            End If

            Dim Precio As String
            Try
                Precio = DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).SelectedItem.Text.Split(" ")(1)
            Catch ex As Exception
                Precio = "$ 0"
            End Try

            If (Precio <> DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text.Replace(".", "").Replace(",", "")) Then

                'insertar Precio
                'Dim RPre As New DotNetResponse.SQLPersistence
                'RPre.AddParametro(Grilla_OC.Items(i).Cells(12).Text)
                'RPre.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).SelectedValue)
                'RPre.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text.Replace(".", ""))
                'RPre.StoredProcedure = "SP_OC_INSERT_PRODUCTO_PRECIO"
                'EXEC.ExecuteNonQuery(RPre)

                Dim RPre As New DotNetResponse.SQLPersistence
                RPre.AddParametro(Grilla_OC.Items(i).Cells(12).Text)
                RPre.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).SelectedValue.Split(" ")(0))
                RPre.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text.Replace(".", "").Replace(",", ""))
                RPre.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(9).Controls(1), Anthem.DropDownList).SelectedItem.Text.Split("-")(1).Replace("x", "").Trim())
                RPre.StoredProcedure = "SP_OC_INSERT_PRODUCTO_PRECIO"
                EXEC.ExecuteNonQuery(RPre)


                If RPre.Errores Then
                    Mensaje("ERROR: No se Puede Registrar el Nuevo Precio, su Información no se ha Grabado")
                    Exit Sub
                End If
            End If

            'Actualizar Producto OC
            ' @cantidad DECIMAL(18,3)
            ',@Precio DECIMAL(18,3)
            ',@Fk_orden_compra NUMERIC(18,0)
            ',@fk_solicitud NUMERIC(18,0)

            Dim RPoc As New DotNetResponse.SQLPersistence
            RPoc.AddParametro(CtNueva)
            RPoc.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text.Replace(".", ""))
            RPoc.AddParametro(Val(NOC.Text))
            RPoc.AddParametro(Grilla_OC.Items(i).Cells(3).Text)
            RPoc.AddParametro(Grilla_OC.Items(i).Cells(12).Text)
            RPoc.StoredProcedure = "SP_OC_UPDATE_OC_PRODUCTO"
            EXEC.ExecuteNonQuery(RPoc)

            If RPoc.Errores Then
                Mensaje("ERROR: No se Puedo Actualizar el Producto, su Información no se ha Grabado")
                Exit Sub
            End If

            'Actualizar Total Cantidad Solicitud           
            ' -- Add the parameters for the stored procedure here  
            ' @RECEPCION DECIMAL(18,3)  
            ',@FK_SOLICITUD NUMERIC(18,0)  
            ',@FK_PRODUCTO INT  
            Dim RPSol_prod As New DotNetResponse.SQLPersistence
            RPSol_prod.AddParametro(CtNueva)
            RPSol_prod.AddParametro(Grilla_OC.Items(i).Cells(3).Text)
            RPSol_prod.AddParametro(Grilla_OC.Items(i).Cells(12).Text)
            RPSol_prod.StoredProcedure = "SP_OC_UPDATE_OC_SOLICITUD_PRODUCTO_OC"
            EXEC.ExecuteNonQuery(RPSol_prod)

            If RPSol_prod.Errores Then
                Mensaje("ERROR: No se Puedo Actualizar el Producto, su Información no se ha Grabado")
                Exit Sub
            End If


        Next
        EXEC.Commit()

        btnBuscaOC_Click(Nothing, Nothing)
        Mensaje("Orden de Compra Actualizada Correctamente")


    End Sub

    Protected Sub Eliminar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        Dim EXEC As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence



        Dim i As Integer = DirectCast(DirectCast(sender.Parent.parent, System.Object), System.Web.UI.WebControls.DataGridItem).ItemIndex
        ' For i = 0 To Me.Grilla_OC.Items.Count - 1

        Dim CtComprada As Decimal = DirectCast(Grilla_OC.Items(i).Cells(7).Controls(1), Label).Text
        Dim CtOC As Decimal = Grilla_OC.Items(i).Cells(14).Text
        Dim CtNueva As Decimal = 0
        Dim CtSolicitud As Decimal = DirectCast(Grilla_OC.Items(i).Cells(6).Controls(1), Label).Text

        If CtComprada - CtOC + CtNueva > CtSolicitud Then
            Mensaje("ERROR: La Cantidad Ingresada Supera el Máximo Permitido")
            EXEC.Rollback()
            Exit Sub
        End If

        'Actualizar Producto OC
        ' @cantidad DECIMAL(18,3)
        ',@Precio DECIMAL(18,3)
        ',@Fk_orden_compra NUMERIC(18,0)
        ',@fk_solicitud NUMERIC(18,0)
        ',@fk_producto NUMERIC(18,0) 

        Dim RPoc As New DotNetResponse.SQLPersistence
        RPoc.AddParametro(CtNueva)
        RPoc.AddParametro(DirectCast(Grilla_OC.Items(i).Cells(10).Controls(1), Anthem.TextBox).Text.Replace(".", ""))
        RPoc.AddParametro(Val(NOC.Text))
        RPoc.AddParametro(Grilla_OC.Items(i).Cells(3).Text)
        RPoc.AddParametro(Grilla_OC.Items(i).Cells(12).Text)
        RPoc.StoredProcedure = "SP_OC_DELETE_OC_PRODUCTO"
        EXEC.ExecuteNonQuery(RPoc)

        If RPoc.Errores Then
            Mensaje("ERROR: No se Puedo Actualizar el Producto, su Información no se ha Grabado")
            Exit Sub
        End If

        'Actualizar Total Cantidad Solicitud           
        ' -- Add the parameters for the stored procedure here  
        ' @RECEPCION DECIMAL(18,3)  
        ',@FK_SOLICITUD NUMERIC(18,0)  
        ',@FK_PRODUCTO INT  
        Dim RPSol_prod As New DotNetResponse.SQLPersistence
        RPSol_prod.AddParametro(CtNueva)
        RPSol_prod.AddParametro(Grilla_OC.Items(i).Cells(3).Text)
        RPSol_prod.AddParametro(Grilla_OC.Items(i).Cells(12).Text)
        RPSol_prod.StoredProcedure = "SP_OC_UPDATE_OC_SOLICITUD_PRODUCTO_OC"
        EXEC.ExecuteNonQuery(RPSol_prod)

        If RPSol_prod.Errores Then
            Mensaje("ERROR: No se Puedo Actualizar el Producto, su Información no se ha Grabado")
            Exit Sub
        End If


        ' Next
        EXEC.Commit()

        btnBuscaOC_Click(Nothing, Nothing)
        Mensaje("Orden de Compra Actualizada Correctamente")


    End Sub

    Protected Sub Cmb_Proveedor_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Precio As String
        Try
            Precio = DirectCast(sender, Anthem.DropDownList).SelectedItem.Text.Split(" ")(1)
        Catch ex As Exception
            Precio = "{$ 0 }"
        End Try


        Dim i As Integer
        DirectCast(DirectCast(sender.parent.parent, DataGridItem).Cells(10).Controls(1), Anthem.TextBox).Text = Precio
        DirectCast(DirectCast(sender.parent.parent, DataGridItem).Cells(10).Controls(1), Anthem.TextBox).ValidationGroup = Precio
        DirectCast(DirectCast(sender.parent.parent, DataGridItem).Cells(10).Controls(1), Anthem.TextBox).UpdateAfterCallBack = True

    End Sub

    Protected Sub Grilla_OC_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grilla_OC.SelectedIndexChanged

    End Sub
End Class